#include "stdafx.h"
#include "HSDSerializeTool.h"

void Seria::Save(CArchive& ar, const bool flag) 
{
    ar << (flag ? 0XFFFF : 0);
}
void Seria::Load(CArchive& ar, bool& flag)
{
    int val;
    ar >> val;
    flag = (val != 0);
}


void Seria::Save(CArchive& ar, const CString& str)
{
	int lenName = str.GetLength();
	ar << lenName;
	ar.Write(const_cast<CString*>(&str)->GetBuffer(), sizeof(char) * lenName);
}

void Seria::Load(CArchive& ar, CString& str)
{
	int ss;
	ar >> ss;
	std::vector<char> chars(ss);
	ar.Read(chars.data(), sizeof(char) * ss);
    str.Format("");
	for (auto c : chars)
	{
        str += c;
	}
}

void Seria::Save(CArchive& ar, const cv::Mat& image)
{
    ar << image.cols;
    ar << image.rows;
    ar << image.type();
    ar.Write(image.data, image.total() *  image.elemSize());
}

void Seria::Load(CArchive& ar, cv::Mat& image)
{
    int cols, rows, type;
    ar >> cols;
    ar >> rows;
    ar >> type;
    if (cols > 0 && rows > 0)
    {
        image = cv::Mat::zeros(cv::Size(cols, rows), type);
        ar.Read(image.data, image.total() * image.elemSize());
    }
    else
    {
        image.release();
    }
}


template <typename _Tp>
void Seria::Save(CArchive& ar, const cv::Point_<_Tp>& pt)
{
    ar << pt.x;
    ar << pt.y;
}

template <typename _Tp>
void Seria::Load(CArchive& ar, cv::Point_<_Tp>& pt)
{
    ar >> pt.x;
    ar >> pt.y;
}

template <typename _Tp>
void Seria::Save(CArchive& ar, const cv::Rect_<_Tp>& rect1)
{
    ar << rect1.x;
    ar << rect1.y;
    ar << rect1.width;
    ar << rect1.height;
}

template <typename _Tp>
void Seria::Load(CArchive& ar, cv::Rect_<_Tp>& rect1)
{
    ar >> rect1.x;
    ar >> rect1.y;
    ar >> rect1.width;
    ar >> rect1.height;
}

template <typename _Tp>
void Seria::Save(CArchive& ar, const std::vector<cv::Point_<_Tp>>& pts)
{
    ar << static_cast<int>(pts.size());
    for (auto& pt : pts)
    {
        ar << pt.x;
        ar << pt.y;
    }
}

template <typename _Tp>
void Seria::Load(CArchive& ar, std::vector <cv::Point_<_Tp>>& pts)
{
    int size;
    ar >> size;
    pts.resize(size);
    for (auto& pt : pts)
    {
        ar >> pt.x;
        ar >> pt.y;
    }
}

template <typename _Tp>
void Seria::Save(CArchive& ar, const std::vector<_Tp>& vals)
{
    ar << static_cast<int>(vals.size());
    for (auto& val : vals)
    {
        ar << val;
    }
}

template <typename _Tp>
void Seria::Load(CArchive& ar, std::vector <_Tp>& vals)
{
    int size;
    ar >> size;
    vals.resize(size);
    for (auto& val : vals)
    {
        ar >> val;
    }
}



